package com.example.administrator.familfinalcialmanagement.bean;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.administrator.familfinalcialmanagement.greendao.gen.DaoMaster;

/**
 * Created by SensYang on 2019/04/01 23:35
 */
public class DBManager {

    public final static String dbName = "test_db";
    public static DBManager mInstance;
    public static DaoMaster.DevOpenHelper openHelper;
    public Context context;

    public DBManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    public static SQLiteDatabase getWritableDatabase(Context context) {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    /**
     * /**
     * 插入一条记录
     *
     * @param user
     */
    //    public void insertUser(User user) {
    //        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
    //        DaoSession daoSession = daoMaster.newSession();
    //        UserDao userDao = daoSession.getUserDao();
    //        userDao.insert(user);
    //    }

    /**
     * 插入用户集合
     */
    //    public void insertUserList(List<User> users) {
    //        if (users == null || users.isEmpty()) {
    //            return;
    //        }
    //        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
    //        DaoSession daoSession = daoMaster.newSession();
    //        UserDao userDao = daoSession.getUserDao();
    //        userDao.insertInTx(users);
    //    }

    /**
     * 删除
     * @param user
     */
    //    public void deleteUser(User user) {
    //        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
    //        DaoSession daoSession = daoMaster.newSession();
    //        UserDao userDao = daoSession.getUserDao();
    //        userDao.delete(user);
    //    }

    /**
     * 更新
     * @param user
     */
    //    public void updateUser(User user) {
    //        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
    //        DaoSession daoSession = daoMaster.newSession();
    //        UserDao userDao = daoSession.getUserDao();
    //        userDao.update(user);
    //    }

    /**
     * 查询用户列表
     */
    //    public List<User> queryUserList() {
    //        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
    //        DaoSession daoSession = daoMaster.newSession();
    //        UserDao userDao = daoSession.getUserDao();
    //        QueryBuilder<User> qb = userDao.queryBuilder();
    //        List<User> list = qb.list();
    //        return list;
    //    }

    /**
     * 查询用户列表
     */
    //    public List<User> queryUserList(int age) {
    //        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
    //        DaoSession daoSession = daoMaster.newSession();
    //        UserDao userDao = daoSession.getUserDao();
    //        QueryBuilder<User> qb = userDao.queryBuilder();
    //        qb.where(UserDao.Properties.Age.gt(age)).orderAsc(UserDao.Properties.Age);
    //        List<User> list = qb.list();
    //        return list;
    //    }
}
